#include "Log.h"

// C headers
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#ifdef WIN32
#define DEBUG_LOG_FILENAME		"log.txt"
#else
#define DEBUG_LOG_FILENAME		"/Applications/log.txt"
#endif

#define LOG_MAX_BUFFER_LEN		1024

// Buffer
char _buf[LOG_MAX_BUFFER_LEN];
FILE * _fh = NULL;

// --------------------------------------------------------
void logToOut(const char * str, ...)
{
	//return;
	va_list argptr; 

	va_start(argptr, str);
	vsprintf(_buf, str, argptr);
	va_end(argptr);

	strcat(_buf, "\n");
	printf(_buf);
}

// --------------------------------------------------------
void logToFile(const char * str, ...)
{
	//return;
	if (!_fh)
		_fh = fopen(DEBUG_LOG_FILENAME, "wb");

	va_list argptr; 

	va_start(argptr, str);
	vsprintf(_buf, str, argptr);
	va_end(argptr);

	strcat(_buf, "\r\n");

	fwrite(_buf, strlen(_buf), sizeof(char), _fh);

}

// --------------------------------------------------------
void logToFileOpen()
{
	//return;
	if (!_fh)
		_fh = fopen(DEBUG_LOG_FILENAME, "wb");
}

// --------------------------------------------------------
void logToFileClose()
{
	//return;
	if (_fh)
		fclose(_fh);
}
